package dao.aplicacao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.Utils;

import modelos.aplicacao.PedidoModel;
import dao.BaseDao;

public class PedidoDao extends BaseDao {

	public PedidoModel getPedido(int id) throws SQLException{
		
		PedidoModel pedido = new PedidoModel();
		
		String sql = "SELECT * FROM `pedidos` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next()){
			
			pedido.setId(id);
			pedido.setMarca(this.getMarca(result.getInt("id_marca")));
			pedido.setModelo(this.getModelo(result.getInt("id_modelo")));
			pedido.setVersao(this.getVersao(result.getInt("id_versao")));
			pedido.setKits(this.getKit(result.getInt("id_kits")));
			pedido.setCor(this.getCor(result.getInt("id_cores")));
			pedido.setValor_total("R$" + new Utils().formataDoubleValorDinheito(result.getDouble("valor_total")));
			pedido.setConcessionarias1(this.getConcessionarias(result.getInt("id_concessionarias1")));
			pedido.setConcessionarias2(this.getConcessionarias(result.getInt("id_concessionarias1")));
			pedido.setConcessionarias3(this.getConcessionarias(result.getInt("id_concessionarias1")));
			pedido.setChave(result.getString("chave_pedido"));
			
		}
		
		return pedido;
	}
	
	public String getMarca(int id) throws SQLException{
		
		String sql = "SELECT `nome` FROM `marca` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next())
			return result.getString("nome");
					
		return "";
		
	}
	
	public String getModelo(int id) throws SQLException{
		
		String sql = "SELECT `nome` FROM `modelo` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next())
			return result.getString("nome");
					
		return "";
		
	}
	
	public String getVersao(int id) throws SQLException{
		
		String sql = "SELECT `nome` FROM `versao` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next())
			return result.getString("nome");
					
		return "";
		
	}
	
	public String getKit(int id) throws SQLException{
		
		String sql = "SELECT `nome` FROM `kit` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next())
			return result.getString("nome");
					
		return "";
		
	}
	
	public String getCor(int id) throws SQLException{
		
		String sql = "SELECT `nome` FROM `kit` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next())
			return result.getString("nome");
					
		return "";
		
	}
	
	public String getConcessionarias(int id) throws SQLException{
		
		String sql = "SELECT `razao_social` FROM `concessionarias` WHERE `id` = "+ id;
		
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet result =  ps.executeQuery();
		
		while(result.next())
			return result.getString("razao_social");
					
		return "";
		
	}
	
}
